package com.example.module_home.house

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_home.R
import com.example.module_home.adapter.MoneyHouseAdapter
import com.example.module_home.databinding.ActivityTenantHouseBinding
import com.example.module_home.vm.HouseViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/TenantHouseActivity")
class TenantHouseActivity : BaseMvvmActivity<ActivityTenantHouseBinding>() {
    var moneyHouseAdapter=MoneyHouseAdapter()
    private val houseViewModel : HouseViewModel by viewModels()

    override fun getLayoutId(): Int =R.layout.activity_tenant_house

    override fun initViews() {

        binding.moneyHouseRv.adapter=moneyHouseAdapter
        houseViewModel.getHouse()
        houseViewModel.mSuccessLiveData.observe(this){
            moneyHouseAdapter.data.addAll(it.data)
            moneyHouseAdapter.notifyDataSetChanged()
        }
        houseViewModel.mErrorLiveData.observe(this){errorMsg->
            Toast.makeText(this,errorMsg, Toast.LENGTH_SHORT).show()
        }


        //搜索
        binding.tenantManageEt.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    houseViewModel.mSuccessLiveData.observe(this@TenantHouseActivity){
                        moneyHouseAdapter.data.clear()
                        moneyHouseAdapter.data.addAll(it.data.filter { it.enterpriseName.contains(query)})
                        moneyHouseAdapter.notifyDataSetChanged()
                    }
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!=null){
                    houseViewModel.mSuccessLiveData.observe(this@TenantHouseActivity){
                        moneyHouseAdapter.data.clear()
                        moneyHouseAdapter.data.addAll(it.data.filter { it.enterpriseName.contains(newText)})
                        moneyHouseAdapter.notifyDataSetChanged()
                    }
                }
                return false
            }
        })

    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }

}